//
//  NetWorkingShop.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/22.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "NetWorkingShop.h"

@implementation NetWorkingShop

//获取TB商城顶部信息
+(void)getTBShopInfoWithSuccessBlock:(void(^)(id responseObj))successBlock{
    [NetWorkTool requestGetWithUrl:[NSString stringWithFormat:@"%@/1",getShopTopsInfo] withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:@"网络错误"];
    }];
}
//获取指南商城顶部信息
+(void)getGuideShopInfoWithSuccessBlock:(void(^)(id responseObj))successBlock{
    [NetWorkTool requestGetWithUrl:GuideMallTop withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:@"网络错误"];
    }];
}

//获取优选商城顶部信息
+(void)getPreferredShopInfoWithSuccessBlock:(void(^)(id responseObj))successBlock{
    [NetWorkTool requestGetWithUrl:[NSString stringWithFormat:@"%@/3",getShopTopsInfo] withSuccessBlock:^(id  _Nonnull responseObj) {
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] showErrorWithTitle:@"网络错误"];
    }];
}

//获取TB商城商品列表
+(void)getTBShopListWithPage:(NSInteger)page WithSuccessBlock:(void(^)(id responseObj))successBlock{
    [[XNProgressHUD shared] showLoadingWithTitle:@"加载中..."];
    NSString *urlStr = [NSString stringWithFormat:@"%@/%ld?typeId=&type=1",TBShopSearch,(long)page];
    [NetWorkTool requestGetWithUrl:urlStr withSuccessBlock:^(id  _Nonnull responseObj) {
        [[XNProgressHUD shared] dismiss];
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] dismiss];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [[XNProgressHUD shared] showErrorWithTitle:errorStr];
        });
    }];
}
//获取优选商城商品列表
+(void)getPreferredShopListWithPage:(NSInteger)page WithSuccessBlock:(void(^)(id responseObj))successBlock{
    [[XNProgressHUD shared] showLoadingWithTitle:@"加载中..."];
    NSString *urlStr = [NSString stringWithFormat:@"%@/%ld?typeId=&type=3",TBShopSearch,(long)page];
    [NetWorkTool requestGetWithUrl:urlStr withSuccessBlock:^(id  _Nonnull responseObj) {
        [[XNProgressHUD shared] dismiss];
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] dismiss];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [[XNProgressHUD shared] showErrorWithTitle:errorStr];
        });
    }];
}
//获取指南商城商品列表
+(void)getGuideShopListWithPage:(NSInteger)page WithSuccessBlock:(void(^)(id responseObj))successBlock{
    [[XNProgressHUD shared] showLoadingWithTitle:@"加载中..."];
    NSString *urlStr = [NSString stringWithFormat:@"%@/%ld",GuideMallList,(long)page];
    [NetWorkTool requestGetWithUrl:urlStr withSuccessBlock:^(id  _Nonnull responseObj) {
        [[XNProgressHUD shared] dismiss];
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] dismiss];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [[XNProgressHUD shared] showErrorWithTitle:errorStr];
        });
    }];
}
//获取商品详情
+(void)getGoodsDetailWithID:(NSInteger)ID WithSuccessBlock:(void(^)(id responseObj))successBlock{
    [[XNProgressHUD shared] showLoadingWithTitle:@"加载中..."];
    NSString *urlStr = [NSString stringWithFormat:@"%@/%ld",ProductDetail,(long)ID];
    [NetWorkTool requestTokenGetWithUrl:urlStr withSuccessBlock:^(id  _Nonnull responseObj) {
        [[XNProgressHUD shared] dismiss];
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] dismiss];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [[XNProgressHUD shared] showErrorWithTitle:errorStr];
        });
    }];
}
//获取商品评价
+(void)getGoodsCommentListWithProductId:(NSInteger)productId WithPage:(NSInteger)page WithSuccessBlock:(void(^)(id responseObj))successBlock{
    [[XNProgressHUD shared] showLoadingWithTitle:@"加载中..."];
    NSString *urlStr = [NSString stringWithFormat:@"%@/%ld/%ld",CommentList,(long)productId,(long)page];
    [NetWorkTool requestGetWithUrl:urlStr withSuccessBlock:^(id  _Nonnull responseObj) {
        [[XNProgressHUD shared] dismiss];
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared] dismiss];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [[XNProgressHUD shared] showErrorWithTitle:errorStr];
        });
    }];
}

//获取商城搜索商品列表
+(void)getShopSearchListWithPage:(NSInteger)page withParams:(NSDictionary *)params WithSuccessBlock:(void(^)(id responseObj))successBlock{
    NSString *urlStr = [NSString stringWithFormat:@"%@/%ld",TBShopSearch,(long)page];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //设置超时
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 20;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    [manager GET:urlStr parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"code"] intValue] != 200) {
            [[XNProgressHUD shared] showErrorWithTitle:responseObject[@"msg"]];
            return;
        }
        successBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error.code  == -1001) {
            [[XNProgressHUD shared] dismiss];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [[XNProgressHUD shared] showErrorWithTitle:@"请求超时,请刷新后再试！"];
            });
            return;
        }
        [[XNProgressHUD shared] showErrorWithTitle:@"网络错误"];
    }];
}


@end






